'''
Created on 2011-9-16

@author: Administrator
'''

from ftplib import FTP
import logging
from os import path

def UploadFile(ftpconfig, templateid, taskid, subid, files, casefiles, mail_addtional_content):
    message = ''
    ftp = FTPInit(ftpconfig)
    if ftp == None:
        return False, 'ftp connect fail'
    
    work_path = "%d_%d" % (templateid, taskid)
    try:
        ftp.mkd(work_path + '/case/' + str(subid))
        ftp.mkd(work_path + '/mail/' + str(subid))
        ftp.mkd(work_path +  '/' + str(subid))
    except Exception as e:
        logging.warn(str(e))
        
    try:
        ftp.cwd(work_path)
    except Exception as e:
        logging.warn(str(e))
        ftp.close()
        return False, 'ftp create dir fail'
    
    for v in files:
        if not path.isfile(v):
            logging.warn("file %s exception", v)
            message += ('\n\t upload %s is not file' % v)
            continue
        f = None
        try:
            f = open(v, 'rb')
            ftp.storbinary('STOR %d/%s' % (subid, path.basename(v)), f)
        except Exception as e:
            logging.exception(e)
            message += ('\n\t upload %s fail' % v)
        finally:
            if f:
                f.close()
    
    for v in casefiles:
        if not path.isfile(v.filename):
            message += ('\n\t upload case result %s is not file' % v)
            continue
        f = None
        try:
            f = open(v.filename, 'rb')
            name  = '(' + v.resluttype + ')' + path.basename(v.filename)
            ftp.storbinary('STOR case/%d/%s' % (subid, name), f)
        except Exception as e:
            logging.exception(e)
            message += ('\n\t upload case result %s fail' % v)
        finally:
            if f:
                f.close()
    
    for v in mail_addtional_content:
        if not path.isfile(v):
            message += ('\n\t upload case result %s is not file' % v)
            continue
        f = None
        try:
            f = open(v, 'rb')
            name  =  path.basename(v)
            ftp.storbinary('STOR mail/%d/%s' % (subid, name), f)
        except Exception as e:
            logging.exception(e)
            message += ('\n\t upload mail addtional %s fail' % v)
        finally:
            if f:
                f.close()
    
    f = None
    try:
        f = open('task.log', 'rb')
        ftp.storbinary('STOR %d/%s' % (subid, 'task.log'), f)
    except Exception as e:
        logging.exception(e)
        message += ('\n\t upload %s fail' % v)
    finally:
        if f:
            f.close()
    ftp.close()
    return True, message

def FTPInit(ftpconfig):
    ftp = FTP()
    try:
        ftp.connect(ftpconfig.ip, ftpconfig.port)
        if ftpconfig.username and ftpconfig.password:
            ftp.login(ftpconfig.username, ftpconfig.password)
        ftp.getwelcome()
    except Exception as e:
        logging.exception(e)
        return None
    return ftp
